import { _decorator, Component, EditBox, Node, director, Label, tween, isValid } from 'cc';
import { NetworkManager } from '../Runtime/NetworkManager';
import { showToast } from '../Runtime/MainManager';
import { ApiMsgEnum } from '../Common';
import DataManager from '../Runtime/DataManager';
import { SceneEnum, UIEnum } from '../Enum';
import { BattleScene } from './BattleScene'; import { UIManager } from '../Runtime/UIManager';
1
const { ccclass, property } = _decorator;

@ccclass('LoginScene')
export class LoginScene extends Component {

    @property(EditBox)
    editBox: EditBox

    @property(Node)
    button: Node

    @property(Label)
    title: Label

    @property(Node)
    buttonSingle: Node

    @property(Node)
    buttonWeapon: Node

    @property(Node)
    buttonProp: Node

    @property(Node)
    buttonTalent: Node

    @property(Node)
    buttonSkill: Node

    async onLoad() {
        this.playEffect()

        // showToast('正在连接服务器')
        this.button.active = false
        this.buttonSingle.active = false
        this.buttonWeapon.active = false
        this.buttonProp.active = false
        this.buttonTalent.active = false
        this.buttonSkill.active = false

        const wait: Promise<unknown>[] = []

        // this.initNet(wait)

        await Promise.all(wait)

        if (NetworkManager.Ins.isConnected) {
            showToast('连接服务器成功')
            this.button.active = true
        } else {
            // showToast('连接服务器失败')
        }

    }
    initNet(wait: Promise<unknown>[]) {
        wait.push(
            NetworkManager.Ins.connect()
                .then(() => {
                    console.log('=== 网络初始化完成')
                })
                .catch(() => {
                    console.log('连接服务器失败')
                })
        )
    }

    onLogin() {
        if (!NetworkManager.Ins.isConnected) {
            showToast('服务器异常')
            return
        }
        if (!this.editBox.string) {
            showToast('请输入昵称')
            return
        }

        NetworkManager.Ins.callApi(ApiMsgEnum.ApiLogin, { nickName: this.editBox.string }).then((data) => {
            const { success, res, error } = data
            if (success) {
                DataManager.Ins.myId = res.player.id
                if (res.isOfflineConnection) {
                    director.loadScene(SceneEnum.Battle, (err, scene) => {
                        if (err) {
                            console.log(err)
                            return
                        }
                        scene.getComponentInChildren(BattleScene).applyOfflineLaunch()
                    })
                } else {
                    director.loadScene(SceneEnum.Hall)
                }
            } else {
                console.log(error)
            }
        })

    }

    onGameStartSingle() {
        director.loadScene(SceneEnum.BattleSingle)
    }

    onWeapon() {

    }
    onProp() { }
    onTalent() {
        UIManager.Ins.show(UIEnum.TalentUI)
    }
    onSkill() {
    }

    playEffect() {
        const color = this.title.color.clone()
        color.a = 0
        this.title.color = color
        const tw = tween(color)
            .to(1.4, { r: color.r, g: color.g, b: color.b, a: 255 }, {
                onUpdate: () => {
                    if (isValid(this.title, true)) {
                        this.title.color = color
                    } else {
                        tw.stop()
                    }
                }
            })
            .call(() => {
                this.buttonSingle.active = true
                // this.buttonWeapon.active = true
                // this.buttonProp.active = true
                this.buttonTalent.active = true
                // this.buttonSkill.active = true
            })
            .start()
    }
}